﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.ServiceModel.DomainServices.Client;
using System.Diagnostics;
using Skynet.BusinessEntities;
namespace Skynet.SLAdministration.Views.MgmCategories
{
    public class MgmCategoriesPresenter : Infrastructure.Silverlight.Views.MasterMVVM
    {
        public DomainDataSource CategoriesDataSource { get; set; }
        [Dependency]
        public Skynet.ServiceImplementation.SkynetDomainContext DomainContext {get;set;}

        public BusinessEntities.ExamCategory WorkCategory { get; set; }
                
        public DelegateCommand<BusinessEntities.ExamCategory> ShowDetailCategoryCommand { get; set; }
        public DelegateCommand<object> NewCategoryCommand { get; set; }
        public DelegateCommand<object> ReloadCommand { get; set; }
        public DelegateCommand<BusinessEntities.ExamCategory> DeleteCategoryCommand { get; set; }
        public MgmCategoriesPresenter()
        {  
        }
        public void ReloadCategories(object param)
        {            
            this.CategoriesDataSource.Load();                        
        }        
        public void ShowDetailCategory(BusinessEntities.ExamCategory exam)
        {
            this.WorkCategory = exam;
            var child = new CreateCategoryView();
            child.Presenter = this;
            child.Show();
            
        }
        public void NewCategory_Execute(object state)
        {
            var cat = new ExamCategory();
             
            this.DomainContext.ExamCategories.Add(cat);
            this.WorkCategory = cat;
            var child = new CreateCategoryView();
            child.Presenter = this;
            child.Show(); 
        }
        public void DeleteCategory(BusinessEntities.ExamCategory category)
        {            
            this.DomainContext.ExamCategories.Remove(category);
            this.DomainContext.SubmitChanges();
        }
        public bool CanDeleteCategory(BusinessEntities.ExamCategory category)
        {
            return true;
        }
        void Categories_EntityRemoved(object sender, EntityCollectionChangedEventArgs<BusinessEntities.ExamCategory> e)
        {
            try
            {
                if (e.Entity.EntityState == EntityState.Deleted)
                {
                    this.DomainContext.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);                
            }
            
        }
        void ExamCategories_EntityAdded(object sender, EntityCollectionChangedEventArgs<BusinessEntities.ExamCategory> e)
        {
            if (e.Entity.EntityState == EntityState.New)
            {
                e.Entity.Id = Guid.NewGuid();
                e.Entity.Title = "insert title";
                e.Entity.Description = "insert description";   
            }
        }
        public override void OnViewReady()
        {
            this.ShowDetailCategoryCommand = new DelegateCommand<BusinessEntities.ExamCategory>(ShowDetailCategory);
            this.DeleteCategoryCommand = new DelegateCommand<BusinessEntities.ExamCategory>(DeleteCategory, CanDeleteCategory);
            this.NewCategoryCommand = new DelegateCommand<object>(NewCategory_Execute);
            this.ReloadCommand = new DelegateCommand<object>(ReloadCategories);


            CategoriesDataSource = new DomainDataSource();
            CategoriesDataSource.DomainContext = this.DomainContext;
            CategoriesDataSource.QueryName = "GetExamCategoriesQuery";

            
            
            this.DomainContext.ExamCategories.EntityAdded += new EventHandler<EntityCollectionChangedEventArgs<BusinessEntities.ExamCategory>>(ExamCategories_EntityAdded);
            this.DomainContext.ExamCategories.EntityRemoved += new EventHandler<EntityCollectionChangedEventArgs<BusinessEntities.ExamCategory>>(Categories_EntityRemoved);
            

            this.InvokePropertyChange("ShowDetailCategoryCommand");
            this.InvokePropertyChange("NewCategoryCommand");
            this.InvokePropertyChange("DeleteCategoryCommand");
            this.InvokePropertyChange("ReloadCommand");
            this.InvokePropertyChange("CategoriesDataSource");
            
            this.ReloadCommand.Execute(null);            
        }
        public void SaveChange()
        {
            this.DomainContext.SubmitChanges();
        }        
    }
}
